//
// Column
//

package net.fauxjobuilder;

public class Column
{
    // ============================================================
    // Fields
    // ============================================================

    private String _rawName;
    private String _name;
    private ColumnType _type;
    private boolean _nullable;
    private boolean _primaryKey;

    // ============================================================
    // Constructors
    // ============================================================

    public Column( String rawName, String name )
    {
        _rawName = rawName;
        _name = name;
        if ( _name == null )
        {
            _name = _rawName.toLowerCase();
        }
    }

    // ============================================================
    // Methods
    // ============================================================

    public String getRawName()
    {
        return _rawName;
    }

    public void setRawName( String rawName )
    {
        _rawName = rawName;
    }

    public String getName()
    {
        return _name;
    }

    /**
     * Underscore plus first letter lowercase. e.g. _numParts
     */
    public String getFieldName()
    {
        char letter = _name.charAt( 0 );
        letter = Character.toLowerCase( letter );

        return "_" + letter + _name.substring( 1 );
    }

    /**
     * First letter lowercase. e.g. numParts
     */
    public String getArgName()
    {
        char letter = _name.charAt( 0 );
        letter = Character.toLowerCase( letter );

        return letter + _name.substring( 1 );
    }

    /**
     * First letter uppercase. e.g. NumParts
     */
    public String getFinderName()
    {
        char letter = _name.charAt( 0 );
        letter = Character.toUpperCase( letter );

        return letter + _name.substring( 1 );
    }

    /**
     * "get" plus first letter uppercase. e.g. getNumParts
     */
    public String getGetterName()
    {
        char letter = _name.charAt( 0 );
        letter = Character.toUpperCase( letter );

        return "get" + letter + _name.substring( 1 );
    }

    /**
     * "set" plus first letter uppercase. e.g. getNumParts
     */
    public String getSetterName()
    {
        char letter = _name.charAt( 0 );
        letter = Character.toUpperCase( letter );

        return "set" + letter + _name.substring( 1 );
    }

    public void setName( String prettyName )
    {
        _name = prettyName;
    }

    public ColumnType getType()
    {
        return _type;
    }

    public void setType( int type )
    {
        _type = ColumnType.find( type );
        assert _type != null : String.format( "Could not find SQL type for %d.", type );
    }

    public void setType( ColumnType type )
    {
        _type = type;
    }

    public boolean isNullable()
    {
        return _nullable;
    }

    public void setNullable( boolean nullable )
    {
        _nullable = nullable;
    }

    public boolean isPrimaryKey()
    {
        return _primaryKey;
    }

    public void setPrimaryKey( boolean primaryKey )
    {
        _primaryKey = primaryKey;
    }

    public String getJavaType()
    {
        return _type.toString( _nullable || _primaryKey );
    }

    public String getStatementSetter()
    {
        return _type.statementSetter();
    }

    @Override
    public int hashCode()
    {
        return _rawName.hashCode();
    }

    @Override
    public boolean equals( Object other )
    {
        if ( other instanceof Column )
        {
            return _rawName.equals( ( (Column)other )._rawName );
        }
        return false;
    }
}

